London | 26-ITP-January | Mouawia Elkhalifa | Sprint 2 | Data Groups#1077
London | 26-ITP-January | Mouawia Elkhalifa | Sprint 2 | Data Groups#1077MouawiaElkhalifa wants to merge 1 commit intoCodeYourFuture:mainfrom
Conversation
| return false; | ||
| } | ||
|
|
||
| return prop in obj; |
There was a problem hiding this comment.
Consider the following two approaches for determining if an object contains a property:
let obj = {}, propertyName = "toString";
console.log( propertyName in obj ); // true
console.log( Object.hasOwn(obj, propertyName) ); // false
Which of these approaches suits your needs better?
For more info, you can look up JS "in" operator vs Object.hasOwn.
| test("return false when paramters invaliud ", () =>{ | ||
| expect(contains([], "a")).toEqual(false) | ||
| }); |
There was a problem hiding this comment.
This test does not yet confirm that the function correctly returns false when the first argument is an array.
This is because contains([], "a") could also return false simply because "a" is not a key of the array.
Arrays are objects, with their indices acting as keys. A proper test should use a non-empty array along with a valid
key to ensure the function returns false specifically because the input is an array, not because the key is missing.
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| const [key, ...valueParts] = pair.split("="); | ||
| queryParams[key] = valueParts.join("="); |
There was a problem hiding this comment.
Note: (No change required)
-
In real query string, both
keyandvalueare percent-encoded or URL encoded in the URL.
For example, the string "5%" is encoded as "5%25". So to get the actual value of "5%25"
(whether it is a key or value in the query string), we need to call a function to decode it. -
You can also explore the
URLSearchParamsAPI.
| const result = {}; | ||
|
|
||
| // count items | ||
| for (const item of arr) { | ||
| if (result[item] === undefined) { | ||
| result[item] = 1; | ||
| } else { | ||
| result[item] = result[item] + 1; | ||
| } | ||
| } |
There was a problem hiding this comment.
Does the following function call returns the value you expect?
tally(["toString", "toString"]);
Suggestion: Look up an approach to create an empty object with no inherited properties.
| if (invertedObj[value] === undefined) { | ||
| invertedObj[value] = key; | ||
| } else if (Array.isArray(invertedObj[value])) { | ||
| invertedObj[value].push(key); | ||
| } else { | ||
| invertedObj[value] = [invertedObj[value], key]; | ||
| } |
There was a problem hiding this comment.
Good job in recognising the possibility of collision and taking an extra step to address it.
Learners, PR Template
Self checklist
Changelist
Implemented all Sprint 2 exercises including:
All functions were tested and verified to meet the expected behavior. Edge cases such as invalid input and duplicate values were handled where required.